﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
using System.IO;
using System.Web.UI.WebControls;

namespace Workshop
{
    public partial class Form1 : Form
    {
        public String ChangeInstructor;
        public Form1()
        {
            InitializeComponent();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void courseInformationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormCourseinformation form = new FormCourseinformation();       //jump to courseInformation page
            form.Show();
            this.Hide();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (txtCourse.Text.Trim() != String.Empty)      //it will not allow to come in when the textbox is empty
            {   XmlDocument xml1 = new XmlDocument();
                xml1.Load("Course.xml");
                XmlNodeList nodelist = xml1.SelectNodes("//CourseID");
                for(int i = 0; i < nodelist.Count; i++){
                    if(nodelist[i].InnerText == txtCourse.Text){
                        if(nodelist[i].ParentNode.ChildNodes[7].InnerText == "IT"){
                            MessageBox.Show("You can not change the IT course here! Please go to 'IT Arrangement' to change it", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                        else if (nodelist[i].ParentNode.ChildNodes[0].Attributes[0].Value != "")
                        {
                            MessageBox.Show("You can not change the course which have time!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }
                }
                if (ChangeInstructor == "" && txtInstructor.Text != "")     //when a course have no teacher, and we want to arrange a teacher for it. Do the following steps
                {
                    CreateNewInstructor();  //this function is for arrange a new teacher to this course
                    UpdateGridView();       //refresh the whole page and table
                }
                //if a course already has a teacher but we want to change the teacher of it
                else if (ChangeInstructor != "" && txtInstructor.Text != "") 
                {
                    int b = 0;  //use for calculate how many time that code has run
                    XmlDocument minusWorkLoad = new XmlDocument();
                    minusWorkLoad.Load("Staff.xml");
                    XmlNodeList nodeList = minusWorkLoad.SelectNodes("//InstructorID");
                    for (int i = 0; i < nodeList.Count; i++)
                    {       
                        if (nodeList[i].InnerText == ChangeInstructor)//find the old teacher's workload because it need to decrease
                        {
                            double workload;
                            double.TryParse(nodeList[i].Attributes[1].Value, out workload);     //change the type from string to integer
                            XmlDocument xml = new XmlDocument();
                            xml.Load("Course.xml");
                            XmlNodeList nodeList2 = xml.SelectNodes("//Course/CourseID");
                            for (int j = 0; j < nodeList2.Count; j++)
                            {   
                                if (nodeList2[j].InnerText == txtCourse.Text) //find that what course old teacher teach
                                {
                                    String CourseName = nodeList2[j].ParentNode.ChildNodes[1].InnerText;    //get the course name which was taught by the old teacher

                                    for (int a = 0; a < nodeList2.Count; a++)
                                    {   //check this course is duplicate or not and check the teacher is duplicate or not，and check the workload have not been minus one 
                                        if (nodeList2[a].ParentNode.ChildNodes[1].InnerText == txtCourseName.Text
                                            && nodeList2[a].ParentNode.ChildNodes[6].InnerText == ChangeInstructor
                                            && b == 0
                                            && nodeList2[a].InnerText != txtCourse.Text)
                                        {
                                            workload = workload - 0.5;      //minus the workload
                                            b++;
                                        }
                                        else if (a == nodeList2.Count - 1 && b != 1)//if search all the node and can not find the duplicate course
                                        {
                                            workload = workload - 1;
                                        }
                                    }
                                }

                            }
                            xml.Save("Course.xml");
                            nodeList[i].Attributes[1].Value = workload.ToString();  //update the workload of old teacher
                            minusWorkLoad.Save("Staff.xml");
                            CreateNewInstructor();      //use the new teacher to replace the old teacher
                            UpdateGridView();       //refresh the whole page
                            ChangeInstructor = "";      //initial the label and text box
                            txtCourse.Text = "";
                            txtCourseName.Text = "";
                            txtInstructor.Text = "";

                        }
                    }
                }
                // if user want to reset the teacher to null
                else if (ChangeInstructor != "" && txtInstructor.Text == "")
                {
                    int b = 0;
                    XmlDocument minusWorkLoad = new XmlDocument();
                    XmlDocument update = new XmlDocument();
                    minusWorkLoad.Load("Staff.xml");
                    update.Load("Course.xml");
                    XmlNodeList nodeList = minusWorkLoad.SelectNodes("//InstructorID");
                    XmlNodeList nodesUpdate = update.SelectNodes("//Course/CourseID");
                    for (int i = 0; i < nodeList.Count; i++)
                    {
                        if (nodeList[i].InnerText == ChangeInstructor)
                        {
                            double workload;
                            double.TryParse(nodeList[i].Attributes[1].Value, out workload);
                            for (int j = 0; j < nodesUpdate.Count; j++)
                            {
                                //it also need to check the teacher is duplicate and the course is duplicate or not
                                if (nodesUpdate[j].ParentNode.ChildNodes[1].InnerText == txtCourseName.Text && nodesUpdate[j].InnerText != txtCourse.Text && b == 0 && nodesUpdate[j].ParentNode.ChildNodes[6].InnerText == ChangeInstructor)
                                {
                                    workload = workload - 0.5;
                                    b++;

                                }
                                else
                                {
                                    if (j == nodesUpdate.Count - 1 && b != 1)   //search all the nodes but can not find this course is duplicate
                                    {
                                        workload = workload - 1;    //it means this course is the only, the workload minus one
                                    }
                                }

                                if (nodesUpdate[j].InnerText == txtCourse.Text) //if we find this course 
                                {
                                    nodesUpdate[j].ParentNode.ChildNodes[6].InnerText = ""; //set the course id and name to null
                                    nodesUpdate[j].ParentNode.ChildNodes[6].Attributes[0].Value = "";
                                }

                            }
                            //label1.Text = b.ToString();
                            update.Save("Course.xml");
                            nodeList[i].Attributes[1].Value = workload.ToString();
                            minusWorkLoad.Save("Staff.xml");
                            UpdateGridView();
                            ChangeInstructor = "";      //initial all the vairable
                            txtCourse.Text = "";
                            txtCourseName.Text = "";
                            txtInstructor.Text = "";
                        }

                    }

                }
                else
                {
                    MessageBox.Show("No content", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("You have to put something in your course box", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            //end
        }

        public void CreateNewInstructor()       //set a teacher to a course
        {
            int b = 0;
            XmlDocument doc = new XmlDocument();
            doc.Load("Staff.xml");
            String path = "//InstructorID";
            XmlNodeList nodeList = doc.SelectNodes(path);
            for (int i = 0; i < nodeList.Count; i++)
            {
                if (nodeList[i].InnerText == txtInstructor.Text)//if the teacher is the one who need to be assigned a course
                {
                    String name = nodeList[i].Attributes[0].Value.ToString();
                    double workload;
                    double.TryParse(nodeList[i].Attributes[1].Value, out workload);
                    if (workload >= 3)      //if the workload is larger than 3, mention the user
                    {
                        MessageBox.Show("The workload of this teacher are larger than 3!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                    XmlDocument xml = new XmlDocument();
                    xml.Load("Course.xml");
                    XmlNodeList nodeList2 = xml.SelectNodes("//Course/CourseID");
                    for (int j = 0; j < nodeList2.Count; j++)
                    {   //if the course name is duplicate but the id is not the same and the following code have not been run. it also need to check the teacher is the same one or not
                        if (nodeList2[j].ParentNode.ChildNodes[1].InnerText == txtCourseName.Text && nodeList2[j].InnerText != txtCourse.Text && b == 0 && nodeList2[j].ParentNode.ChildNodes[6].InnerText == txtInstructor.Text)
                        {
                            workload = workload + 0.5;
                            b++;
                        }
                        else
                        {
                            if (j == nodeList2.Count - 1 && b != 1)
                            {
                                workload = workload + 1;
                            }
                        }
                        if (nodeList2[j].InnerText == txtCourse.Text)
                        {
                            nodeList2[j].ParentNode.ChildNodes[6].InnerText = txtInstructor.Text;   //set a teacher name for this course
                            nodeList2[j].ParentNode.ChildNodes[6].Attributes[0].Value = name;

                        }
                    }
                    xml.Save("Course.xml");
                    nodeList[i].Attributes[1].Value = workload.ToString();      //update workload
                    doc.Save("Staff.xml");
                    break;
                }
            }
            UpdateGridView();
            ChangeInstructor = "";
            txtCourse.Text = "";
            txtCourseName.Text = "";
            txtInstructor.Text = "";
            //end

        }

        private void dataGridViewIC_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            comboInstructor.Text = dataGridViewIC.CurrentRow.Cells[1].Value.ToString();
            //comboCourse.Text = dataGridViewIC.CurrentRow.Cells[3].Value.ToString(); 
            txtInstructor.Text = dataGridViewIC.CurrentRow.Cells[0].Value.ToString();
            txtCourse.Text = dataGridViewIC.CurrentRow.Cells[2].Value.ToString();
            txtCourseName.Text = dataGridViewIC.CurrentRow.Cells[3].Value.ToString();
            ChangeInstructor = txtInstructor.Text; //used for save the old instructor who will be changed
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
            UpdateComboInstructor();
            UpdateGridView();
        }

     public static void SetIDWid(DataGridView dataGridView1)        //set the different size of different table column 
        {
            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                if (dataGridView1.Columns[i].HeaderCell.Value.ToString() == "InstructorID")
                {
                    dataGridView1.Columns[i].Width = 145;
                    dataGridView1.Columns[i].Resizable = DataGridViewTriState.False;
                }
                if (dataGridView1.Columns[i].HeaderCell.Value.ToString() == "CourseID")
                {
                    dataGridView1.Columns[i].Width = 130;
                    dataGridView1.Columns[i].Resizable = DataGridViewTriState.False;
                }
                if (dataGridView1.Columns[i].HeaderCell.Value.ToString() == "Instructor")
                {
                    dataGridView1.Columns[i].Width = 130;
                    dataGridView1.Columns[i].Resizable = DataGridViewTriState.False;
                }
            }
        }

        private void comboInstructor_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtInstructor.Text = comboInstructor.SelectedValue.ToString();
        }

        public void UpdateComboInstructor()     //used for showing the instructor in the combo list
        {
            XmlDocument xml = new XmlDocument();
            xml.Load("Staff.xml");
            XmlNodeList nodeList = xml.SelectNodes("//Instructors/InstructorID");
            DataTable table = new DataTable();
            DataColumn column1 = new DataColumn("ID", typeof(string));
            DataColumn column2 = new DataColumn("Instructor", typeof(string));
            table.Columns.Add(column1);
            table.Columns.Add(column2);
            DataRow kong = table.NewRow();
            kong[0] = "";           //the true value
            kong[1] = "Reset";      //just the name
            table.Rows.Add(kong);
            for (int i = 0; i < nodeList.Count; i++)
            {
                DataRow row = table.NewRow();
                row[0] = nodeList[i].InnerText; 
                row[1] = nodeList[i].Attributes[0].Value.ToString();
                table.Rows.Add(row);
            }
            comboInstructor.DisplayMember = "Instructor";
            comboInstructor.ValueMember = "ID";
            comboInstructor.DataSource = table;
        }


        private void UpdateGridView()
        {
            //show the data in DataGridViewIW
            XmlDocument xml1 = new XmlDocument();
            xml1.Load("Staff.xml");
            DataTable tableIW = new DataTable();
            DataColumn col1 = new DataColumn("Instructor", typeof(string));
            DataColumn col2 = new DataColumn("Workload", typeof(string));
            tableIW.Columns.Add(col1);
            tableIW.Columns.Add(col2);
            XmlNodeList nodeListIW = xml1.SelectNodes("//Instructors/InstructorID");
            if (nodeListIW != null)
            {
                for (int i = 0; i < nodeListIW.Count; i++)
                {
                    DataRow row = tableIW.NewRow();
                    row[0] = nodeListIW[i].Attributes[0].Value.ToString();
                    row[1] = nodeListIW[i].Attributes[1].Value.ToString();
                    tableIW.Rows.Add(row);
                }
            }
            dataGridViewIW.DataSource = tableIW;

            //show the data in DataGridViewIC
            XmlDocument xml2 = new XmlDocument();
            xml2.Load("Course.xml");
            DataTable tableIC = new DataTable();
            DataColumn col3 = new DataColumn("InstructorID", typeof(string));
            DataColumn col4 = new DataColumn("Instructor", typeof(string));
            DataColumn col5 = new DataColumn("CourseID", typeof(string));
            DataColumn col6 = new DataColumn("Course", typeof(string));
            tableIC.Columns.Add(col3);
            tableIC.Columns.Add(col4);
            tableIC.Columns.Add(col5);
            tableIC.Columns.Add(col6);
            XmlNodeList nodeListIC = xml2.SelectNodes("//Course/InstructorID");
            if (nodeListIC != null)
            {
                for (int j = 0; j < nodeListIC.Count; j++)
                {
                    DataRow row = tableIC.NewRow();
                    row[0] = nodeListIC[j].InnerText;
                    row[1] = nodeListIC[j].Attributes[0].Value.ToString();
                    row[2] = nodeListIC[j].ParentNode.ChildNodes[0].InnerText;
                    row[3] = nodeListIC[j].ParentNode.ChildNodes[1].InnerText;
                    tableIC.Rows.Add(row);

                }
            }
            
            dataGridViewIC.DataSource = tableIC;
            this.dataGridViewIC.Columns["InstructorID"].Visible = false;
            this.dataGridViewIC.Columns["CourseID"].Visible = false;
            //SetIDWid(dataGridViewIC);
        }

        private void courseTimeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormITgroups form = new FormITgroups();     //jump to courseTime page
            form.Show();
            this.Hide();
        }

        private void instructorInfomationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormAddinstructor form = new FormAddinstructor();       //jump to instructorInformation page
            form.Show();
            this.Hide();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void txtCourse_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtInstructor_TextChanged(object sender, EventArgs e)
        {

        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            FormHeadPixel headPixel = new FormHeadPixel();
            headPixel.Show();
            this.Close();
        }


    }
}
